﻿#pragma once

#include <QObject>
#include "BaseShader.h"
/// <summary>
/// 调节亮度、曝光度、对比度、饱和度的Shader
/// </summary>
class BrightnessExposureContrastSaturationShader : public BaseShader
{
	Q_OBJECT

public:
	/*static BrightnessExposureContrastSaturationShader* Get()
	{
		static BrightnessExposureContrastSaturationShader becss;
		return &becss;
	}*/
	BrightnessExposureContrastSaturationShader(QObject* parent = nullptr);
	~BrightnessExposureContrastSaturationShader();
private:
	
	//顶点着色器代码
	QString vertexSource = R"(
	attribute vec4 vertexIn;
	attribute vec2 textureIn;
	varying vec2 textureOut;
	void main(void)
	{
		gl_Position = vertexIn;
		textureOut = textureIn;
	}
	)";

	//片元着色器代码
	QString fragmentSource = R"(
	varying vec2 textureOut;
	uniform sampler2D tex_y;
	uniform sampler2D tex_u;
	uniform sampler2D tex_v;
	uniform float brightness;  // 亮度 (-1.0~1.0)
	uniform float exposure;    // 曝光 (-1.0~1.0)
	uniform float contrast;    // 对比度 (0.0~2.0, 默认1.0)
	uniform float saturation;  // 饱和度 (0.0~2.0, 默认1.0)
	void main(void)
	{
		vec3 yuv;
		vec3 rgb;
		yuv.x = texture2D(tex_y, textureOut).r;
		yuv.y = texture2D(tex_u, textureOut).r - 0.5;
		yuv.z = texture2D(tex_v, textureOut).r - 0.5;
		rgb = mat3(1.0, 1.0, 1.0,
			0.0, -0.39465, 2.03211,
			1.13983, -0.58060, 0.0) * yuv;
		 // 1. 亮度调节
		rgb += brightness;
    
		// 2. 曝光调节
		rgb *= pow(2.0, exposure);
    
		// 3. 对比度调节
		rgb = (rgb - 0.5) * contrast + 0.5;
    
		// 4. 饱和度调节
		float luminance = dot(rgb, vec3(0.2126, 0.7152, 0.0722));
		rgb = mix(vec3(luminance), rgb, saturation);
    
		// 确保颜色值在有效范围内
		rgb = clamp(rgb, 0.0, 1.0);
		gl_FragColor = vec4(rgb,1.0);
	}
	)";
};
